// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "Interfaces/IHttpRequest.h"
#include "HotChunkFile.h"
#include "HotChunkSave.h"
#include "HotChunkComponent.generated.h"

class UHotChunkSave;
class UChunkPendingContent;


DECLARE_DELEGATE_OneParam(FHotChunkTextDelegate, FString)

DECLARE_DELEGATE(FHotChunkActionDelegate)

DECLARE_DELEGATE_OneParam(FHotChunkProgressDelegate, float)

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class HOTCHUNK_API UHotChunkComponent : public USceneComponent
{
	GENERATED_BODY()

public:
	// Sets default values for this component's properties
	UHotChunkComponent();

	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	void MainProcess();

	void DoTryAgain();

public:

	FHotChunkTextDelegate CurrentVersionSumbit;

	FHotChunkTextDelegate NewVersionSumbit;

	FHotChunkTextDelegate HotChunkStateSumbit;

	FHotChunkActionDelegate ShowOpenMenuAction;

	FHotChunkActionDelegate ShowReCheckAction;

	FHotChunkProgressDelegate HotChunkProgressSumbit;

	UPROPERTY(EditAnywhere)
		TArray<FString> VersionWebSites;

	UPROPERTY(EditAnywhere)
		FString VersionWebName;

	UPROPERTY(EditAnywhere)
		FString TempStoragePath;

	UPROPERTY(EditAnywhere)
		FString ChunkStoragePath;

	UPROPERTY(EditAnywhere)
		FString HotChunkSaveName;

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

	void CheckAndClearHotChunk();

	void RequireVersionWebCompleted(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool IsSucceeded);

	void RequirePlatformManifestCompleted(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool IsSucceeded);

	void RequestChunkContentSucceeded(UChunkPendingContent* ResponseContent);

	void RequestChunkContentFailed(FText ErrorText, int32 ErrorCode);

	void InstallChunkContentSucceeded();

	void InstallChunkContentFailed(FText ErrorText, int32 ErrorCode);

	void SaveHotChunkStatus(EHotChunkStatus HotChunkStatus);

protected:

	UPROPERTY()
		UHotChunkSave* HotChunkSave;

	FString TempStorageFolder;

	FString ChunkStorageFloder;

	TSharedPtr<FVersionWebInfo> VersionWebInfo;

	TSharedPtr<FVersionManifestInfo> CurrentVersionManifestInfo;

	TSharedPtr<FVersionManifestInfo> NewVersionManifestInfo;

	TArray<TSharedPtr<FChunkManifestInfo>> DifferChunkManifestInfos;

	TArray<TSharedPtr<FChunkManifestInfo>> AbandonChunkManifestInfos;

	UPROPERTY()
		TArray<UChunkPendingContent*> ChunkPendingContents;

	int32 RequireWebSiteIndex;

	int32 RequireResSiteIndex;

	int32 RequireChunkContentIndex;

	int32 InstallChunkContentIndex;

	float TotalRequireDiskSpace;

	int32 RetryInstallCount;

};

